package org.sam.swing.table.tools;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;

import org.apache.commons.lang3.tuple.Pair;
import org.jdesktop.swingx.JXTable;
import org.sam.swing.JSDialog;
import org.sam.swing.table.model.FilterObject;

/**
 * 过滤筛选窗口
 * 
 * @author sam
 *
 */
public class JDialogFilter extends JSDialog {

	private static final long serialVersionUID = 5612175559135812504L;

	/**
	 * 过滤窗口
	 */
	protected JPanelFilter filterPanel;

	/**
	 * 当前的被操作表格对象
	 */
	protected JXTable table;

	/**
	 * 当前的被操作表格对象
	 * 
	 * @return
	 */
	public JXTable getTable() {
		return table;
	}

	/**
	 * 当前的被操作表格对象
	 * 
	 * @param table
	 */
	public void setTable(JXTable table) {
		this.table = table;
	}

	/**
	 * 过滤的条件集合
	 */
	protected List<FilterObject> filterObjects;

	/**
	 * 过滤筛选窗口
	 * 
	 * @param table
	 */
	public JDialogFilter(JXTable table, List<FilterObject> filterObjects) {
		this.setTable(table);
		this.setLayout(new BorderLayout());
		this.setTitle("过滤条件");
		this.filterObjects = filterObjects;
		
		JPanel compent = this.buildCompent();
		if (compent != null){
			this.add(compent , BorderLayout.CENTER);
		}
		
		JPanel actions = this.buildActionCompent();
		if (actions != null){
			this.add(actions , BorderLayout.SOUTH);
		}
	}

	/**
	 * 生成过滤条件对话框窗口
	 * 
	 * @param table
	 *            表格对象
	 * @param filterObjects
	 *            过滤数据
	 * @return
	 */
	public static List<FilterObject> showFilterDialog(JXTable table, List<FilterObject> filterObjects) {
		JDialogFilter dialog = new JDialogFilter(table, filterObjects);
		dialog.setModal(true);
		dialog.setSize(640, 480);
		dialog.setLocationRelativeTo(null);
		dialog.setVisible(true);

		return dialog.filterPanel.getFilterObjects();
	}
	
	/**
	 * 生成过滤条件对话框窗口，并返回用户按钮状态
	 * @param table
	 * @param filterObjects
	 * @return
	 */
	public static Pair<String , List<FilterObject>> showFilterDialogWidthAction(JXTable table, List<FilterObject> filterObjects){
		JDialogFilter dialog = new JDialogFilter(table, filterObjects);
		dialog.setModal(true);
		dialog.setSize(640, 480);
		dialog.setLocationRelativeTo(null);
		dialog.setVisible(true);

		return Pair.of(dialog.getAction(), dialog.filterPanel.getFilterObjects());
	}

	/**
	 * 生成中间的控件区域
	 */
	public JPanel buildCompent() {
		filterPanel = new JPanelFilter(this.getTable(), this.getTable().getModel(), this.getTable().getColumnModel(),
				filterObjects);
		return filterPanel;
	}

	/**
	 * {@inheritDoc} 生成操作对象
	 */
	public JPanel buildActionCompent() {
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));

		JButton btnOk = new JButton("确定");
		btnOk.setName("btnOk");
		btnOk.addActionListener(e -> {
			this.setAction(OK);
			try {
				filterPanel.getFilterTableModel().update();
				this.setVisible(false);
			} catch (Exception e1) {
				e1.printStackTrace();
			}

		});
		panel.add(btnOk);

		JButton btnCancel = new JButton("取消");
		btnCancel.setName("btnCancel");
		btnCancel.addActionListener(e -> {
			this.setAction(CANCEL);
			this.setVisible(false);
		});
		panel.add(btnCancel);

		return panel;
	}

}
